package com.chtw.utils;

import java.util.Collection;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class POIUtils {
	public static HSSFWorkbook expExcel(JSONArray head, JSONArray body,String sheetname) {
		// 创建一个excel工作簿
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 创建一个sheet工作表
		HSSFSheet sheet = workbook.createSheet(sheetname);

		// 创建第0行表头，再在这行里在创建单元格，并赋值
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = null;
		if (head != null) {
			for (int i = 0; i < head.size(); i++) {
				cell = row.createCell(i);
				cell.setCellValue(head.getString(i));// 设置值
			}
		}
		// list<pojo>
		// 将主体数据填入Excel中
		for (int i = 0, isize = body.size(); i < isize; i++) {
			row = sheet.createRow(i + 1);
			// JSONArray stuInfo = body.getJSONArray(i);
			JSONObject obj = body.getJSONObject(i);

			Collection<Object> values = obj.values();
			Object[] array = new Object[values.size()];
			values.toArray(array);

			for (int j = 0, jsize = array.length; j < jsize; j++) {
				cell = row.createCell(j);
				cell.setCellValue(array[j].toString());// 设置值
			}
		}
		return workbook;
	}
}
